//
//  MADareViewController.m
//  MateAdv
//
//  Created by 茶 on 2023/9/11.
//

#import "MADareViewController.h"
#import "MADareView.h"

@interface MADareViewController () <UINavigationControllerBackButtonHandlerProtocol>

@property (nonatomic, strong) MADareView *dareView;

@property (nonatomic, strong) NSMutableArray *truthArray;
@property (nonatomic, strong) NSMutableArray *dareArray;

@property (nonatomic, assign) BOOL isShowResult;

@end

@implementation MADareViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initInterface];
    _isShowResult = NO;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [_dareView roundAnimationEvent];
}

- (void)initInterface {
    self.view.backgroundColor = MAColor(@"#FF0071");
    self.title = @"真心话大冒险";
    
    [self.view addSubview:self.dareView];
    [_dareView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
}
- (BOOL)shouldPopViewControllerByBackButtonOrPopGesture:(BOOL)byPopGesture {
    if (_isShowResult) {
        MAWeakify(self);
        [LEEAlert alert].config
        .LeeTitle(@"提示")         // 添加一个标题 (默认样式)
        .LeeContent(@"是否退出当前游戏")        // 添加一个标题 (默认样式)
        .LeeAction(@"取消", ^{        //添加一个默认类型的Action (默认样式 字体颜色为蓝色)
        })
        .LeeCancelAction(@"确定", ^{    // 添加一个取消类型的Action (默认样式 alert中为粗体 actionsheet中为最下方独立)
            MAStrongify(self);
            [self.navigationController popViewControllerAnimated:YES];
        })
        .LeeShow();
        return NO;
    }
    return YES;
}


- (void)showResultView:(NSUInteger)type {
    [self initDataSource];
    NSMutableArray *array = (type == 1 ? self.dareArray : self.truthArray);
    NSUInteger index = 0;
    if (array.count > 0) {
        index = arc4random() % array.count;
    }
    MAHomeRuleModel *model = array[index];
    [self.dareView showResultView:model.content];
    [array removeObjectAtIndex:index];
    
}

- (void)initDataSource {
    if (self.dareArray.count == 0) {
        for (MAHomeRuleModel *model in _model.ruleList) {
            if (model.type.intValue == 3) {
                [_dareArray addObject:model];
            }
        }
    }
    if (self.truthArray.count == 0) {
        for (MAHomeRuleModel *model in _model.ruleList) {
            if (model.type.intValue == 2) {
                [_truthArray addObject:model];
            }
        }
    }
    
}

- (MADareView *)dareView {
    if (_dareView) return _dareView;
    _dareView = [MADareView new];
    _dareView.fText = self.fName;
    _dareView.mText = self.mName;
    _dareView.type = self.type;
    MAWeakify(self);
    _dareView.choiceBlock = ^(NSUInteger type) {
        MAStrongify(self);
        [self showResultView:type];
    };
    _dareView.endBlock = ^{
        MAStrongify(self);
        [self.dareView roundAnimationEvent];
        self.isShowResult = NO;
    };
    _dareView.showChoiceBlock = ^{
        MAStrongify(self);
        self.isShowResult = YES;
    };
    return _dareView;
}

- (NSMutableArray *)truthArray {
    if (_truthArray) return _truthArray;
    _truthArray = [NSMutableArray array];
    return _truthArray;
}

- (NSMutableArray *)dareArray {
    if (_dareArray) return _dareArray;
    _dareArray = [NSMutableArray array];
    return _dareArray;
}

@end
